//
//  ZhuHouseXVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/4/26.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "ZhuHouseXVC.h"

#import "DetailsViewController.h"
#import "TodoHttpManager.h"
#import "OneCustomCell.h"
#import "TwoCustomCell.h"
#import "CCZTableButton.h"
#import "NeiLiuView.h"
#import "ComputerHouserVC.h"

#import "MJRefresh.h"
@interface ZhuHouseXVC ()<UITableViewDelegate,UITableViewDataSource,NeiLiuViewDelegate>

{
   
    NSString *aStr;
    NSString *bStr;
    NSString *cStr;
    NSString *dStr;
    NSString *eStr;
    NSString *fStr;
    NSString *gStr;
    NSString *hStr;
    NSString *iStr;
    NSString *jStr;
    NSString *selesdStr;
    NSMutableArray *_btnTitleArr;
    NSString *liuTime;
    NSString *liuNameStr;//预留姓名
    NSString *zhuStr;//备注
    NSString *chouStr;//认筹优惠

}
@property(nonatomic,strong)UITableView *tabView;
@property (nonatomic, strong) CCZTableButton *table2;
@property (nonatomic, strong)NSMutableArray *zuoArr;
@property(nonatomic,strong)NSMutableArray *youArr;
@property(nonatomic,strong)NSMutableDictionary *allDic;
@property(nonatomic,strong)NSMutableArray *xuanArr;
@property(nonatomic,strong)NSMutableDictionary *oneDic;

@property (nonatomic, strong) NeiLiuView *alertView;

@end

@implementation ZhuHouseXVC

-(void)viewWillAppear:(BOOL)animated
{
    //房源优惠
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didZhuSYouHui:) name:TDZhuSYouHui object:nil];
    [_manager userZhuShouYouHuiWithID:_fangID withType:@"1" withStates:nil];
    
    //房源预留
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDZhuSYuLiu:) name:TDZhuSYuLiu object:nil];
}

//房源优惠
-(void)didZhuSYouHui:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        int status = [[notifi.object objectForKey:@"status"] intValue];
        if (status == 1)
        {
            _allDic = [notifi.object objectForKey:@"data"];
           NSLog(@"总的字典内容----houseDic==%@",_allDic);
            
            aStr = [_allDic objectForKey:@"name"];
            bStr = [_allDic objectForKey:@"sale_area"];
            cStr = [_allDic objectForKey:@"sale_price"];
            dStr = [_allDic objectForKey:@"sale_price_all"];
            
            _xuanArr = [_allDic objectForKey:@"data"];
            
            _oneDic = [_xuanArr objectAtIndex:0];//按揭
            eStr = [_oneDic objectForKey:@"pay_way"];
            chouStr = [_oneDic objectForKey:@"renchou_discount"];
            fStr = [_oneDic objectForKey:@"cash_discount"];
            gStr = [_oneDic objectForKey:@"area_discount"];
            hStr = [_oneDic objectForKey:@"discount"];
            iStr = [_oneDic objectForKey:@"xieyi_price_all"];
            jStr = [_oneDic objectForKey:@"xieyi_price"];
            
            
            liuTime = [_allDic objectForKey:@"yuliu_time"];
            liuNameStr = [_allDic objectForKey:@"yuliu_name"];
            zhuStr = [_allDic objectForKey:@"yuliu_beizhu"];
            
            _youArr = [NSMutableArray arrayWithObjects:aStr,bStr,cStr,dStr,eStr,chouStr,fStr,gStr,hStr,iStr,jStr, liuTime,liuNameStr,zhuStr,nil];
         //   NSLog(@"lastarr 按揭支付 =========%@",_youArr);
            
            
            
            [_tabView reloadData];
        }

    }
    
}
 //房源预留

-(void)didTDZhuSYuLiu:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        int statue = [[notifi.object objectForKey:@"status"] intValue];
        if (statue == 1)
        {
            [_manager MakeMyselfAlert:@"成功" andIsNeedTitle:NO andShowTime:2.0];
            
           // DetailsViewController *detailsVc = [[DetailsViewController alloc] init];
            [self.navigationController popViewControllerAnimated:NO];
          [_tabView reloadData];
        }
        else
        {
            [_manager MakeMyselfAlert:@"失败" andIsNeedTitle:NO andShowTime:2.0];
        }

    }
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDZhuSYouHui object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDZhuSYuLiu object:nil];

}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.title = @"房源管理";
    self.view.backgroundColor = BgColor;
    _manager = [TodoHttpManager sharedManager];
    NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
    if ([IDA isEqualToString:@"2"]&&[_Is_Sale isEqualToString:@"0"])
    {
        UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(neiLiuBtnClick)];
        
        self.navigationItem.rightBarButtonItem = rightBarItem;
        
        [self.navigationItem.rightBarButtonItem setTitle:@"内留"];
        [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
        
        
    }
    if ([_Is_Sale isEqualToString:@"3"])
    {
        _zuoArr = [[NSMutableArray alloc] initWithObjects:@"房源位置 :",@"销售面积 :",@"销售表价 :",@"表价总价 :",@"付款方式 :",@"认筹优惠 :",@"总价优惠 :",@"单价优惠 :",@"折扣优惠 :",@"协议总价 :",@"协议单价 :",@"预留日期 :",@"预留姓名 :",@"备注 :", nil];
    }
    else
    {
        _zuoArr = [[NSMutableArray alloc] initWithObjects:@"房源位置 :",@"销售面积 :",@"销售表价 :",@"表价总价 :",@"付款方式 :",@"认筹优惠 :",@"总价优惠 :",@"单价优惠 :",@"折扣优惠 :",@"协议总价 :",@"协议单价 :", nil];
    }
    
    _youArr = [NSMutableArray arrayWithCapacity:0];

    
    _allDic = [NSMutableDictionary dictionary];
    _xuanArr = [NSMutableArray array];
    _oneDic = [NSMutableDictionary dictionary];
    
    _btnTitleArr = [NSMutableArray arrayWithObjects:@"按揭支付",@"全款支付", nil];



    
    _tabView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT) style:UITableViewStyleGrouped];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 40;
    _tabView.backgroundColor = BgColor;
    
    //_tabView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tabView];
    

}


- (NeiLiuView *)alertView {
    if (!_alertView) {
        self.alertView = [[NeiLiuView alloc] initWithFrame:CGRectMake(30, 200, [UIScreen mainScreen].bounds.size.width - 60, 220)];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.delegate = self;
    }
    return _alertView;
}

-(void)neiLiuBtnClick
{
       [self.alertView showView];//弹出
}


- (void)requestEventAction:(UIButton *)button {
    
    [self.alertView closeView];

    
    [_manager userZhuShouYuLiuWithID:_fangID withType:@"1" withLiuName:self.alertView.msgField.text withLiuTime:self.alertView.oneLab.text withBeiZhu:self.alertView.xiaLab.text];

    [self.tabView reloadData];
    
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _zuoArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 4)
    {
        NSString *cellIndent = @"CellA";
        TwoCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndent];
        if (cell ==nil)
        {
            cell = [[TwoCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndent];
        }
        
        cell.aneTLab.text= @"付款方式:";
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//取消点击
        
        CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
        CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];
        cell.oneBtn.frame = rect;
        
        UIButton*deviceImageButton = cell.oneBtn;
        [deviceImageButton addTarget:self action:@selector(deviceButtonPressedClick:)
                    forControlEvents:UIControlEventTouchUpInside];
        self.table2 = [[CCZTableButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(rect)+100, CGRectGetMaxY(rect), 260, 0)];
        [self.table2 addItems:_btnTitleArr exceptItem:@"全部"];
        [self.table2 selectedAtIndexHandle:^(NSUInteger index, NSString *itemName) {
            
            [cell.oneBtn setTitle:itemName forState:UIControlStateNormal];
      
            if (index == 1)
            {
                
                [cell.oneBtn setTitle:@"全款支付" forState:UIControlStateNormal];
                _oneDic = [_xuanArr objectAtIndex:1];//按揭
                eStr = [_oneDic objectForKey:@"pay_way"];
                   chouStr = [_oneDic objectForKey:@"renchou_discount"];
                fStr = [_oneDic objectForKey:@"cash_discount"];
                gStr = [_oneDic objectForKey:@"area_discount"];
                hStr = [_oneDic objectForKey:@"discount"];
                iStr = [_oneDic objectForKey:@"xieyi_price_all"];
                jStr = [_oneDic objectForKey:@"xieyi_price"];
                
                liuTime = [_allDic objectForKey:@"yuliu_time"];
                liuNameStr = [_allDic objectForKey:@"yuliu_name"];
                zhuStr = [_allDic objectForKey:@"yuliu_beizhu"];
                
                _youArr = [NSMutableArray arrayWithObjects:aStr,bStr,cStr,dStr,eStr,chouStr,fStr,gStr,hStr,iStr,jStr, liuTime,liuNameStr,zhuStr,nil];
                //NSLog(@"lastarr 全款支付=========%@",_lastArr);
                [self.tabView reloadData];
                
            }
            else
            {
                [cell.oneBtn setTitle:@"按揭支付" forState:UIControlStateNormal];
                _oneDic = [_xuanArr objectAtIndex:0];//按揭
                eStr = [_oneDic objectForKey:@"pay_way"];
                   chouStr = [_oneDic objectForKey:@"renchou_discount"];
                fStr = [_oneDic objectForKey:@"cash_discount"];
                gStr = [_oneDic objectForKey:@"area_discount"];
                hStr = [_oneDic objectForKey:@"discount"];
                iStr = [_oneDic objectForKey:@"xieyi_price_all"];
                jStr = [_oneDic objectForKey:@"xieyi_price"];
                
                
                liuTime = [_allDic objectForKey:@"yuliu_time"];
                liuNameStr = [_allDic objectForKey:@"yuliu_name"];
                zhuStr = [_allDic objectForKey:@"yuliu_beizhu"];
                
                _youArr = [NSMutableArray arrayWithObjects:aStr,bStr,cStr,dStr,eStr,chouStr,fStr,gStr,hStr,iStr,jStr, liuTime,liuNameStr,zhuStr,nil];
               NSLog(@"lastarr 按揭支付 =========%@",_youArr);
                [self.tabView reloadData];
            }
            
            
            
            
            
        }];
        return cell;
        
    }
    else
    {
        NSString * cellIndent = @"CellB";
        OneCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndent];
        if (cell ==nil)
        {
            cell = [[OneCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndent];
            
            
        }
        if ([cell isKindOfClass:[OneCustomCell class]])
        {
            if (_allDic.count != 0)
            {
                
                NSString *str0 = (NSString *)_youArr[indexPath.row];
                cell.twoLab.text = [NSString stringWithFormat:@"%@",str0];
                
                // NSLog(@"jiciya-----%@",_lastArr);
                
            }
            
        }
        
        
        cell.oneLab.text = _zuoArr[indexPath.row];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//取消点击
        
        
        return cell;
        
    }
    
     

}

-(void)deviceButtonPressedClick:(id)sender
{
    
    UIView *v = [sender superview];//获取父类view
    TwoCustomCell *cell = (TwoCustomCell *)[v superview];//获取cell
    NSIndexPath *indexpath = [self.tabView indexPathForCell:cell];//获取cell对应的indexpath;
    NSLog(@"%@",indexpath);
    
    [self.table2 show];
    
    //  NSLog(@"设备图片按钮被点击:%ld%ld",(long)indexpath.section,(long)indexpath.row);
    
    
    
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if ([_Is_Sale isEqualToString:@"0"])
    {
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 40)];
        footView.backgroundColor = [UIColor whiteColor];
        
        [self.view addSubview:footView];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10, 5, 200, 30);
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft ;
        [btn setTitle:@"房贷计算器" forState:0];
        if ([jStr isEqualToString:@""]||[iStr isEqualToString:@""])
        {
            [btn setUserInteractionEnabled:NO];
            
        }
        [btn setTitleColor:[UIColor redColor] forState:0];
        [btn setImage:[UIImage imageNamed:@"jisuanqi"] forState:0];
        [btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:5];
        [btn addTarget:self action:@selector(computerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [footView addSubview:btn];
        return footView;
    }
    else
    {
        return 0;
    }
  
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 40;
}

-(void)computerBtnClick:(UIButton *)btn
{

//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"缺少信息" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
//    [alertView show];
    ComputerHouserVC *computerVC = [[ComputerHouserVC alloc] init];
    computerVC.chDIc = _allDic;
    [self.navigationController pushViewController:computerVC animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
